var buySet = {
    init: function () {
        this.layuiUse();
        $('button[name="clickBtn"]').on('click', function () {
            buySet.checkPassword();
        });
    },
    layuiUse: function () {
        layui.use(['layer', 'form', 'jquery', 'table','laydate','element'], function () {
            var layer = layui.layer,
                form = layui.form,
                $ = layui.$,
                table = layui.table,
                element = layui.element,
                laydate = layui.laydate;
                
                if(GetCookie('teaAdminCheckPassword')){
                    $('div[name="checkBox"]').addClass('hide');
                    $('div[name="tableListBox"]').removeClass('hide');
                }else{
                    $('div[name="checkBox"]').removeClass('hide');
                    $('div[name="tableListBox"]').addClass('hide');
                }
                $('.layui-body').css('width',document.body.clientWidth-200);
                //回显次数
                if(GetCookie('teaAdminCheckPassword')){
                    $.ajax({
                        type: 'post',
                        url: link + '/admin/sysSet/query/buyNum',
                        data: {
                            'token': GetCookie('teaAdminCheckPassword'),
                        },
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if(res.code==10000){
                                $('input[name="number"]').val(res.entity);
                            }else{
                                layui.use(['layer'], function () {
                                    var layer = layui.layer;
                                    layer.msg(res.message);
                                });
                            }
                        },
                        error: function (res) {
                            layui.use(['layer'], function () {
                                var layer = layui.layer;
                                layer.msg(res.message);
                            });
                        },
                    });
                }
                
                // 设置次数
                form.on('submit(formSubmit)', function (data) {
                    console.log(data.field);
                    $.ajax({
                        type: 'post',
                        url: link + '/admin/sysSet/update/buyNum',
                        data: {
                            'token': GetCookie('teaAdminCheckPassword'),
                            'value':data.field.number
                        },
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if(res.code==10000){
                                layui.use(['layer'], function () {
                                    var layer = layui.layer;
                                    layer.msg(res.message);
                                });
                            }else{
                                layui.use(['layer'], function () {
                                    var layer = layui.layer;
                                    layer.msg(res.message);
                                });
                            }
                        },
                        error: function (res) {
                            layui.use(['layer'], function () {
                                var layer = layui.layer;
                                layer.msg(res.message);
                            });
                        },
                    });
                    return false;
                });
            
        });
    },

    //判断是否需要输入动态口令
    checkPassword: function () {
        $.ajax({
            type: 'post',
            url: link + '/admin/checkToken',
            data: {
                'token': $('#pwd').val(),
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.entity.flag) {
                    SetCookieDomain('teaAdminCheckPassword', res.entity.token, '');
                    $('div[name="checkBox"]').addClass('hide');
                    $('div[name="tableListBox"]').removeClass('hide');
                    location.reload();
                } else {
                    layui.use(['layer'], function () {
                        var layer = layui.layer;
                        layer.msg('动态口令输入错误！');
                    });
                }
            },
            error: function (res) {
                layui.use(['layer'], function () {
                    var layer = layui.layer;
                    layer.msg(res.message);
                });
            },
        });
    },
};
buySet.init();